cmake_minimum_required(VERSION 3.15)

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}   /MP")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MD")
set(LIBRARY_OUTPUT_PATH    ${CMAKE_SOURCE_DIR}/bin)
endif ()

set(CWD ${CMAKE_CURRENT_LIST_DIR})

project(meeting_service)


#the thread library of the system.
find_package(Threads REQUIRED)

include(../cmake/build.cmake)

SET(ENABLE_SSL ON)
SET(ENABLE_CLIENT_SSL ON)

if (ENABLE_SSL)
	add_definitions(-DCINATRA_ENABLE_SSL)
	message(STATUS "Use SSL")
    find_package(OpenSSL REQUIRED)
    include_directories(${OPENSSL_INCLUDE_DIR})
endif()

if(ENABLE_CLIENT_SSL)
	add_definitions(-DCINATRA_ENABLE_CLIENT_SSL)
endif()

add_definitions(-DASIO_STANDALONE)



include_directories(${CWD})
    include_directories(../libs/include)
    include_directories(../libs/include/ormpp)
    include_directories(${CWD}/controller)
    include_directories(${CWD}/model)
    include_directories(${CWD}/service)
    include_directories(${CWD}/http)
    include_directories(${CWD}/db)
    include_directories(${CWD}/net)
    include_directories(${CWD}/utils)
    include_directories(${CWD}/sqlite3)

    link_directories(../libs/lib_release)

    FILE (GLOB _files
            ${CWD}/*.cpp
            ${CWD}/*.hpp
            )

    FILE(GLOB controller_files
           ${CWD}/controller/*.hpp
            ${CWD}/controller/*.cpp
            )

	FILE(GLOB model_files
           ${CWD}/model/*.hpp
            ${CWD}/model/*.cpp
            )

     FILE(GLOB client_files
            ${CWD}/client/*.hpp
            ${CWD}/client/*.cpp
            )

     FILE(GLOB utils_files
           ${CWD}/utils/*.h
           ${CWD}/utils/*.hpp
           ${CWD}/utils/*.cpp
            )

     FILE(GLOB net_files
           ${CWD}/net/*.h
           ${CWD}/net/*.hpp
           ${CWD}/net/*.cpp
        )
    FILE(GLOB http_files
           ${CWD}/http/*.hpp
           ${CWD}/http/*.cpp
        )
     FILE(GLOB service_files
           ${CWD}/service/*.hpp
           ${CWD}/service/*.cpp
        )

     FILE(GLOB conf_files
           ${CWD}/conf/*.hpp
           ${CWD}/conf/*.cpp
        )

		FILE(GLOB db_files
           ${CWD}/db/*.h
           ${CWD}/db/*.hpp
           ${CWD}/db/*.cpp
        )
		
		FILE(GLOB sqlite3_files
           ${CWD}/sqlite3/*.h
           ${CWD}/sqlite3/*.c
          )

   source_group("Source Files/controller" FILES ${controller_files})
   source_group("Source Files/client" FILES ${client_files})
   source_group("Source Files/utils" FILES ${utils_files})
   source_group("Source Files/net" FILES ${net_files})
   source_group("Source Files/service" FILES ${service_files})
   source_group("Source Files/conf" FILES ${conf_files})
   source_group("Source Files/db" FILES ${db_files})
   source_group("Source Files/sqlite3" FILES ${sqlite3_files})
   source_group("Source Files/model" FILES ${model_files})
   source_group("Source Files/http_files" FILES ${http_files_files})


SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH    ${CMAKE_SOURCE_DIR}/bin)

 add_library(meeting_service
   SHARED
${_files} ${controller_files} ${client_files}  ${utils_files} ${net_files} ${service_files} ${conf_files} ${db_files} ${sqlite3_files} ${model_files} ${http_files} )

#  add_executable(meeting_service
#  
# ${_files} ${controller_files} ${client_files}  ${utils_files} ${net_files} ${service_files} ${conf_files} ${db_files} ${sqlite3_files} ${model_files} ${http_files})


if(ENABLE_SIMD STREQUAL "SSE42" OR ENABLE_SIMD STREQUAL "AVX2" OR ENABLE_SIMD STREQUAL "AARCH64")
	if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
		message(STATUS "Build with simd in aarch64")
		add_definitions(-DCINATRA_ARM_OPT)
	elseif (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
		message(STATUS "Build with simd in X86_64")
		if (ENABLE_SIMD STREQUAL "SSE42")
			message(STATUS "Build with SSE4.2 ISA")
			add_definitions(-DCINATRA_SSE)
		elseif (ENABLE_SIMD STREQUAL "AVX2")
			message(STATUS "Build with AVX2 ISA")
			add_definitions(-DCINATRA_AVX2)
		endif ()
	endif ()
endif()



add_definitions(-DCORO_HTTP_PRINT_REQ_HEAD)






if (ENABLE_SIMD STREQUAL "AARCH64")
	if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
		add_library(neon INTERFACE IMPORTED)
		target_compile_options(neon INTERFACE -march=armv8-a+fp+simd)
		target_link_libraries(meeting_service neon)
	endif ()
elseif (ENABLE_SIMD STREQUAL "SSE42")
	if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
		add_library(sse4_2 INTERFACE IMPORTED)
		if(MSVC)
			target_compile_options(sse4_2 INTERFACE /arch:SSE4.2)
		else()
			target_compile_options(sse4_2 INTERFACE -msse4.2)
		endif()
		target_link_libraries(meeting_service sse4_2)
	endif ()
elseif (ENABLE_SIMD STREQUAL "AVX2")
	if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
		add_library(avx2 INTERFACE IMPORTED)
		if(MSVC)
			target_compile_options(avx2 INTERFACE /arch:AVX2)
		else()
			target_compile_options(avx2 INTERFACE -mavx2)
		endif()
		target_link_libraries(meeting_service avx2)
		set(CMAKE_CXX_FLAGS "-fpermissive")
	endif ()
endif ()

target_compile_definitions(meeting_service PRIVATE ASYNC_SIMPLE_HAS_NOT_AIO)

if (ENABLE_SSL)
	target_link_libraries(meeting_service PUBLIC ${OPENSSL_LIBRARIES})
endif()

target_link_libraries(meeting_service PRIVATE  
    libxmlrpc++
    libxmlrpc
    libxmlrpc_abyss++
    libxmlrpc_abyss
    libxmlrpc_client
    libxmlrpc_server
    libxmlrpc_server_abyss
    libxmlrpc_server_cgi
    libxmlrpc_server_w32httpsys
    libxmlrpc_util++
    libxmlrpc_util
    libxmlrpc_xmlparse
    libxmlrpc_xmltok
    libzmq-v143-mt-4_3_5
)

if (MSVC)
    target_link_libraries(meeting_service PRIVATE Ws2_32 legacy_stdio_definitions Dnsapi iphlpapi winmm crypt32 ncrypt)
endif ()


